{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[0, 1, 2, 3, 4],\n",
       "       [0, 1, 2, 3, 4],\n",
       "       [0, 1, 2, 3, 4]])"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# numpy是一个高效的运算工具库，主要采用其中的ndarray数据结构，是一个多维数组\n",
    "# 与原生list提供相同的功能，但是更高效\n",
    "# 区别：1. 原生list同一个数组支持不同数据类型，ndarray只能是同一类类型\n",
    "#       2. 由于原生类型可以存储不同的数据类型所以列表中存储的是引用，ndarray中存储的是相同类型的可以顺序存储\n",
    "#       3. 实现上，ndarray采用c语言实现，去掉了GIL锁实现向量化(并行)计算\n",
    "# 更多参照numpy文档\n",
    "import numpy as np\n",
    "\n",
    "nar = np.array([range(5),range(5),range(5)])  # 二维数组\n",
    "nar"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "sum list ->  1.0203680992126465\n",
      "np.sum -> 0.00799703598022461\n"
     ]
    }
   ],
   "source": [
    "# 效率对比， 居然差100多倍\n",
    "import time\n",
    "mlist = range(10000000) # 1kw数\n",
    "t1 = time.time()\n",
    "s = sum(mlist)\n",
    "print('sum list -> ', time.time() - t1)\n",
    "\n",
    "ndar = np.array(mlist)\n",
    "t2 = time.time()\n",
    "s = np.sum(ndar)\n",
    "print('np.sum ->', time.time() - t2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "(3, 5) 2 15 int32 4\n"
     ]
    }
   ],
   "source": [
    "print(nar.shape, nar.ndim, nar.size, nar.dtype, nar.itemsize) # 数组维度行列， 维度， 大小， 类型， 元素大小"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "(5,) 1 5 int64 8\n"
     ]
    }
   ],
   "source": [
    "nar = np.array([1,2,3,4,5], dtype=np.int64) # 一维数组, 可以指定数据类型，不指定默认int32\n",
    "# nar = np.array([1,2,3,4,5], dtype='int64') # 与上面等效\n",
    "print(nar.shape, nar.ndim, nar.size, nar.dtype, nar.itemsize)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[0, 0, 0],\n",
       "       [0, 0, 0]])"
      ]
     },
     "execution_count": 14,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 数组基本方法使用\n",
    "# 生成数组0-1占位\n",
    "np.zeros((2,3), dtype=np.int32) # 生成两行三列的数组，0填充所有位置"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[1, 1, 1],\n",
       "       [1, 1, 1]])"
      ]
     },
     "execution_count": 15,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.ones((2,3), dtype=np.int32) # 生成两行三列的数组，1填充所有位置"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 30,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[0 1 2 3 4]\n",
      " [0 1 2 3 4]]\n",
      "========\n",
      "[[0 1 2 3 4]\n",
      " [0 1 2 3 4]]\n",
      "========\n",
      "[[0 1 2 3 4]\n",
      " [0 1 2 3 4]]\n",
      "[[0 1 2 3 4]\n",
      " [0 1 2 3 4]]\n",
      "========\n",
      "[[0 1 2 3 4]\n",
      " [0 1 2 3 4]]\n",
      "========\n",
      "[[ 0  1  2  3  4]\n",
      " [ 0  1 10  3  4]]\n"
     ]
    }
   ],
   "source": [
    "# 数组创建\n",
    "arr = np.array([range(5), range(5)])\n",
    "ar1 = np.array(arr) # 深度拷贝\n",
    "ar2 = np.copy(arr) # 深度拷贝\n",
    "ar3 = np.asarray(arr) # 浅拷贝\n",
    "print(ar1, ar2,  ar3, sep='\\n========\\n')\n",
    "arr[1,2] = 10 # ar3也将变化\n",
    "print(ar1, ar2,  ar3, sep='\\n========\\n')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 32,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([ 0. ,  2.5,  5. ,  7.5, 10. ])"
      ]
     },
     "execution_count": 32,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 生成范围内数据\n",
    "np.linspace(0,10,5) # [0-10]生成5个数，则间隔为2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 34,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([0, 2, 4, 6, 8])"
      ]
     },
     "execution_count": 34,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 生成范围内数据\n",
    "np.arange(0, 10, 2) # 生成[0-10)的数，步长间隔2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 35,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([ 0.76480523, -0.60675819, -0.17023997, ..., -0.43397221,\n",
       "       -0.33077085,  0.90359883])"
      ]
     },
     "execution_count": 35,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 生成随机数\n",
    "# 均匀分布的随机数\n",
    "d1 = np.random.uniform(-1,1, 10000) # -1到1生成1w个数, 符合均匀分布\n",
    "d1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 40,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "iVBORw0KGgoAAAANSUhEUgAAA6sAAAIICAYAAACIHXhxAAAABHNCSVQICAgIfAhkiAAAAAlwSFlzAAALEgAACxIB0t1+/AAAADh0RVh0U29mdHdhcmUAbWF0cGxvdGxpYiB2ZXJzaW9uMy4xLjMsIGh0dHA6Ly9tYXRwbG90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jb8a4qPAa+e306yp6qumd7J25/kyLTsSJID0+UDSZbyTi0Xtpz79Jzfm5h+aH/FX0uy6FHUWLaLzktVva6qvveVy0n+Sv70/vdYWX1LmZNK8uEkJ8YYHzhrmcfK6rnQ68QrjiT52ekIjjck+fr08e2lbMvyXfR+rao3JflEkp8ZY/zezPiFnsuYz1Lm5Qem565U1fVZ+Jn2a0vZlhVZ0v1aVd+X5C9l5rXGY2VdbfzXlPU+wtNGOGXhB7RTSV5M8nySB6fxNyb51Mx6t2ThKJpfycLHh18Z/7NJjiZ5cjq/Yr2/p41+Ot99usicvDYLL17fd9b2/yHJl5N8KQsPzh3r/T1thtNS5iULR5774nQ67rHSYk5+LAsf//lSki9Mp1umZR4rqzsf57xOJPm5JD83Xa4kvzwt/3JmjkB/vtcYp0s+Jx9K8oczj41j0/h5n8uc1mRe3jPd71/MwoGv3nahbZ0u/ZxM1/9GkvvO2s5j5dLNyceSnE7y7Sy0yu2b7TWlpp0FAACANnwMGAAAgHbEKgAAAO2IVQAAANoRqwAAALQjVgEAAGhHrAIAANCOWAUAAKAdsQoAAEA7/x+RyYBxyNmDgQAAAABJRU5ErkJggg==\n",
      "text/plain": [
       "<Figure size 1152x648 with 1 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    }
   ],
   "source": [
    "import matplotlib.pyplot as plt\n",
    "plt.figure(figsize=(16,9))\n",
    "plt.hist(d1, 10) # 数据分10组\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 47,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "iVBORw0KGgoAAAANSUhEUgAAA6UAAAIICAYAAACW1EjCAAAABHNCSVQICAgIfAhkiAAAAAlwSFlzAAALEgAACxIB0t1+/AAAADh0RVh0U29mdHdhcmUAbWF0cGxvdGxpYiB2ZXJzaW9uMy4xLjMsIGh0dHA6Ly9tYXRwbG90bGliLm9yZy+AADFEAAAYcklEQVR4nO3df4xl9Xnf8c8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v2Z335jkxiTZv3//K6IVgO9f+w7ePj0CALBLbOTqu5Xks0kOd/enVz10W5JrFrevSfKVVdsPVNUbquq8JOcnuWfrRgYAAGC32MiR0vcm+WCSB6rq/sW2Tya5IcmtVfWRJI8n+UCSdPdDVXVrkoezcuXe67r7+S2fHAAAgB1v3Sjt7t/I2u8TTZJLX+U51ye5fhNzAQAAcBJ4XVffBQAAgK0kSgEAABgjSgEAABgjSgEAABgjSgEAABgjSgEAABgjSgEAABgjSgEAABgjSgEAABgjSgEAABgjSgEAABgjSgEAABgjSgEAABgjSgEAABgjSgEAABgjSgEAABgjSgEAABgjSgEAABgjSgEAABgjSgEAABgjSgEAABgjSgEAABgjSgEAABgjSgEAABgjSgEAABgjSgEAABgjSgEAABgjSgEAABgjSgEAABgjSgEAABgjSgEAABgjSgEAABgjSgEAABgjSgEAABgjSgEAABgjSgEAABgjSgEAABgjSgEAABgjSgEAABgjSgEAABgjSgEAABgjSgEAABgjSgEAABgjSgEAABgjSgEAABgjSgEAABgjSgEAABgjSgEAABgjSgEAABgjSgEAABgjSgEAABgjSgEAABgjSgEAABgjSgEAABgjSgEAABgjSgEAABgjSgEAABgjSgEAABgjSgEAABgjSgEAABgjSgEAABgjSgEAABgjSgEAABgjSgEAABgjSgEAABgjSgEAABgjSgEAABgjSgEAABgjSgEAABgjSgEAABgjSgEAABgjSgEAABgjSgEAABgjSgEAABgjSgEAABgjSgEAABgjSgEAABgjSgEAABgjSgEAABgjSgEAABgjSgEAABgjSgEAABizbpRW1U1V9XRVPbhq289W1e9V1f2Lj/eteuwTVXWkqh6pqsuWNTgAAAA730aOlH4uyeVrbP+F7r5o8XFHklTVBUkOJLlw8ZzPVNUpWzUsAAAAu8u6Udrddyf5zga/3pVJbunuZ7v70SRHkly8ifkAAADYxTbzntKPVtU3F6f3nrbYdnaSJ1btc3Sx7RWq6tqqOlRVh44fP76JMQAAANipTjRKfznJO5JclORYkp9fbK819u21vkB339jd+7t7/969e09wDAAAAHayE4rS7n6qu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3v/3r17t3gMAAAAdoITjdKnquqsJFl8fnqx/WiSc1ftd06SJ098PAAAAHazE43S25Jcs7h9TZKvrNp+oKreUFXnJTk/yT2bGxEAAIDdas96O1TVF5JckuT0qjqa5FNJbkhya1V9JMnjST6QJN39UFXdmuThJM8lua67n1/S7AAAAOxw60Zpd1/9Kg9d+ir7X5/k+s0MBQAAwMlhqy90BAAAABsmSgEAABgjSgEAABgjSgEAABgjSgEAABgjSgEAABgjSgEAABgjSgEAABgjSgEAABgjSgEAABgjSgEAABgjSgEAABgjSgEAABgjSgEAABgjSgEAABgjSgEAABgjSgEAABgjSgEAABgjSgEAABgjSgEAABgjSgEAABgjSgEAABgjSgEAABgjSgEAABgjSgEAABgjSgEAABgjSgEAABgjSgEAABgjSgEAABgjSgEAABgjSgEAABgjSgEAABgjSgEAABgjSgEAABgjSgEAABgjSgEAABgjSgEAABgjSgEAABgjSgEAABgjSgEAABgjSgEAABgjSgEAABgjSgEAABgjSgEAABgjSgEAABgjSgEAABgjSgEAABgjSgEAABgjSgEAABgjSgEAABgjSgEAABgjSgEAABgjSgEAABgjSgEAABgjSgEAABgjSgEAABgjSgEAABgjSgEAABgjSgEAABgjSgEAABgjSgEAABgjSgEAABgjSgEAABgjSgEAABgjSgEAABgjSgEAABgjSgEAABgjSgEAABgjSgEAABgjSgEAABgjSgEAABgjSgEAABgjSgEAABgjSgEAABgjSgEAABgjSgEAABgjSgEAABgjSgEAABgjSgEAABgjSgEAABgjSgEAABizZzNPrqrHknwvyfNJnuvu/VX11iT/Psm+JI8l+Xvd/QebGxMAAIDdaCuOlP7N7r6ou/cv7h9Mcld3n5/krsV9AAAAeIVlnL57ZZKbF7dvTnLVEl4DAACAXWCzUdpJfr2q7q2qaxfbzuzuY0my+HzGJl8DAACAXWpT7ylN8t7ufrKqzkhyZ1X9zkafuIjYa5Pk7W9/+ybHAAAAYCfa1JHS7n5y8fnpJF9OcnGSp6rqrCRZfH76VZ57Y3fv7+79e/fu3cwYAAAA7FAnHK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IfGZ6JGQfi1N2T1Yezchp/d/eRJI8meefwTGyjxb8HPtzdFyX5UJK9WVkH7CJVdWpWYuTz3f2lNXY5muTcVffPycoZFOwSG1gDW0aUsmFV9bbFe4hePF3nB5L8/uxUbLOvJPlrVbVncfrme7LyHgNOIlX15iR/IyvrgZPP41k5WyKL9xH+xSTfGp2IbVVVb6mqH1zc/QdJ7nakfHdZ/Hvvs0kOd/enX2W325J8aHEV3h9N8ofdfWzbhmSpNrgGtozTd3lJVX0hySVJTq+qo0k+leTUJOnuf5Pk7yb5h1X1XJL/m+RAu3zzrrLeGujuw1X11STfTPJCkn/b3a/662PYeTbwfSBJ3p/k17v7j0aGZKk2sAb+eZLPVdUDWTl972cWZ06wS2xgDbwrya9W1fNJHk7ykaFRWZ73JvlgkgcWp2knySeTvD15aR3ckZUr7x5J8n+ychYFu8e6a6Cq3pbkUJI/n+SFqvp4kgtO5IdUfiUMAAAAY5y+CwAAwBhRCgAAwBhRCgAAwBhRCgAAwBhRCgAAwBhRCgAAwBhRCgAAwBhRCgAAwJj/D3LTRISSvY1MAAAAAElFTkSuQmCC\n",
      "text/plain": [
       "<Figure size 1152x648 with 1 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    }
   ],
   "source": [
    "# 生成随机数 符合正太分布的随机数, 正态分布就是一组数据中接近中间的数据最多，两边的最少，例如学生成绩，中间的多，好的和差的少\n",
    "d2 = np.random.normal(loc=1.8, scale=0.1, size=1000) # 生成100个数据，标准差0.1， 以1.8身高为平均值, size还可以是个tuple可以生成多维随机数\n",
    "\n",
    "plt.figure(figsize=(16,9))\n",
    "plt.hist(d2, 10) \n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 55,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[0.96378786 1.05183996 1.00107747 0.91141671 1.2402314 ]\n",
      " [0.98495985 1.16436231 0.91956279 1.07329025 0.82869349]\n",
      " [0.78597479 0.9158977  1.06389114 0.97099576 0.92797788]] (3, 5)\n",
      "---------------------------------\n",
      "[[0.96378786 1.05183996 1.00107747]\n",
      " [0.91141671 1.2402314  0.98495985]\n",
      " [1.16436231 0.91956279 1.07329025]\n",
      " [0.82869349 0.78597479 0.9158977 ]\n",
      " [1.06389114 0.97099576 0.92797788]] (5, 3)\n",
      "[[0.96378786 1.05183996 1.00107747 0.91141671 1.2402314 ]\n",
      " [0.98495985 1.16436231 0.91956279 1.07329025 0.82869349]\n",
      " [0.78597479 0.9158977  1.06389114 0.97099576 0.92797788]] (3, 5)\n",
      "---------------------------------\n",
      "[[0.96378786 1.05183996 1.00107747]\n",
      " [0.91141671 1.2402314  0.98495985]\n",
      " [1.16436231 0.91956279 1.07329025]\n",
      " [0.82869349 0.78597479 0.9158977 ]\n",
      " [1.06389114 0.97099576 0.92797788]] (5, 3)\n",
      "---------------------------------\n",
      "[[0.96378786 0.91141671 1.16436231 0.82869349 1.06389114]\n",
      " [1.05183996 1.2402314  0.91956279 0.78597479 0.97099576]\n",
      " [1.00107747 0.98495985 1.07329025 0.9158977  0.92797788]] (3, 5)\n",
      "---------------------------------\n"
     ]
    }
   ],
   "source": [
    "# 数组行列转换\n",
    "d1 = np.random.normal(loc=1, scale=0.1, size=(3,5)) # 3行5列的随机数\n",
    "print(d1, d1.shape)\n",
    "print('-'*33)\n",
    "d2 = d1.reshape((5,3)) # 只是把行列数切分成5行3列了，返回切分后的数组，并没有反转\n",
    "print(d2, d2.shape)\n",
    "print(d1, d1.shape)\n",
    "print('-'*33)\n",
    "d1.resize((5,3)) # 没返回值，原基础切分与reshape效果一样\n",
    "print(d1, d1.shape)\n",
    "print('-'*33)\n",
    "print(d1.T, d1.T.shape) # T就是行列反转后的数组\n",
    "print('-'*33)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 69,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "(2, 4) int32\n",
      "(2, 4) int64\n",
      "[1 2 3 4 5 6]\n",
      "{1, 2, 3, 4, 5, 6}\n"
     ]
    }
   ],
   "source": [
    "# 类型转换与去重\n",
    "tmp = np.array([range(1,5), range(3,7)])\n",
    "print(tmp.shape, tmp.dtype)\n",
    "tmp = tmp.astype(np.int64)\n",
    "print(tmp.shape, tmp.dtype)\n",
    "print(np.unique(tmp)) # 去重\n",
    "print(set(tmp.flatten())) # set去重，将数组展开为一维数组"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 86,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[False False  True False False]\n",
      " [False  True False  True False]\n",
      " [ True False False False False]]\n",
      "[[False False  True]\n",
      " [False  True False]]\n",
      "[[0.91219109 0.89626982 1.00215992 0.93799945 0.96587708]\n",
      " [0.99970726 1.10449494 0.9214734  1.         1.        ]\n",
      " [1.00294231 0.95047292 0.98576306 1.         1.        ]]\n",
      "False\n",
      "True\n",
      "[[False False  True False False]\n",
      " [False False False False False]\n",
      " [ True False False False False]]\n",
      "[[False  True  True False False]\n",
      " [False  True False False False]\n",
      " [ True False False False False]]\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "array([[1, 1, 0, 1, 1],\n",
       "       [1, 0, 1, 0, 0],\n",
       "       [0, 1, 1, 0, 0]])"
      ]
     },
     "execution_count": 86,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 逻辑运算\n",
    "d1 = np.random.normal(loc=1, scale=0.1, size=(3,5)) # 3行5列的随机数\n",
    "print(d1 > 1) # 布尔索引，该操作将返回布尔数组与原数据对应，如果原数据>1则为True否则False\n",
    "print(d1[:2, :3] > 1) # 前两个数组的前三个元素条件布尔集合\n",
    "d1[1:3, 3:5] = 1 # 1-2数组的3-5个元素赋值为1\n",
    "print(d1)\n",
    "print(np.all(d1 > 1)) # 是否数组的所有元素都满足元素>1\n",
    "print(np.any(d1 > 1)) # 是否数组的存在元素满足元素>1\n",
    "\n",
    "print(np.logical_and(d1 > 1, d1 < 1.1)) # 逻辑与\n",
    "print(np.logical_or(d1 > 1, d1 < 0.9)) # 逻辑或\n",
    "\n",
    "np.where(d1 >= 1, 0, 1) # 数组中>=1的位置置为0，否则置为1\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 95,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1.2680909618363512 1.2680909618363512\n",
      "[0 2 1 2 1]\n",
      "[1.09629001 1.26809096 1.14235223 1.02717434 1.1202807 ] [1.09629001 1.16599679 1.26809096]\n",
      "1.0327134679473255 0.12000836137099188 0.014402006798950578\n",
      "[1.07088992 1.16599679 1.05021578 1.01379455 0.94783613]\n"
     ]
    }
   ],
   "source": [
    "# 统计运算 min max median mean std var, 这些方法可以直接通过ndarray对象.方法()调用也可以通过np.方法()调用\n",
    "d1 = np.random.normal(loc=1, scale=0.1, size=(3,5))\n",
    "print(np.max(d1), d1.max())\n",
    "print(np.argmax(d1, axis=0)) # 获取最大值所在位置的索引\n",
    "print(d1.max(axis=0), d1.max(axis=1)) # 按照列计算每列最大，按行计算每行最大值 \n",
    "print(d1.mean(), d1.std(), d1.var()) # 平均数 标准差 \n",
    "print(np.median(d1, axis=0)) # 中位数"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 103,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[[1, 2, 3],\n",
       "        [2, 3, 4],\n",
       "        [3, 4, 5]],\n",
       "\n",
       "       [[1, 2, 3],\n",
       "        [2, 3, 4],\n",
       "        [3, 4, 5]]])"
      ]
     },
     "execution_count": 103,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 数组间的运算\n",
    "ar1 = np.array([range(3), range(3)]) # shape (2,3) 两行三列\n",
    "ar1 * 3 # 数组中每个元素乘以3，数组的算术运算是numpy独有，原生数组不支持\n",
    "# 数组与数组运算\n",
    "# 满足广播机制：\n",
    "#   1. 同纬度的维度信息相等\n",
    "#   2. 同纬度的维度信息有一个是1的\n",
    "ar1 = np.array([range(3), range(3)]) \n",
    "ar2 = np.array([range(5, 8), range(8, 11)])\n",
    "ar1 * ar2  # ar1和ar2两个数组维度都是(2, 3), 两行三列\n",
    "ar3 = np.array([[6], [4]]) # shape维度(2, 1), 两行1列\n",
    "ar1 * ar3 # ar1和ar3分别是(2, 3) (2, 1), 维度倒着对齐，最后一位3，1满足有一个是1的，倒数第二位2，2相同，因此满足广播机制可以数组计算\n",
    "\n",
    "ar4 = np.array([[range(3), range(3), range(3)],[range(3), range(3), range(3)]]) # shape (2,3,3) 三维数组\n",
    "ar5 = np.array([[1],[2],[3]]) # shape (3,1)\n",
    "ar4 + ar5 \n",
    "# 倒着对比\n",
    "#  (2,3,3)\n",
    "#  (  3,1) \n",
    "# 倒数第一位满足有一个是1维，倒数第二位都是3相同，因此可以两个数组进行计算"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 129,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[22 28]\n",
      " [40 52]]\n",
      "[[22 28]\n",
      " [40 52]]\n",
      "[[22 28]\n",
      " [40 52]]\n",
      "[[22 28]\n",
      " [40 52]]\n"
     ]
    }
   ],
   "source": [
    "# 矩阵运算\n",
    "# 矩阵是二位数组，二维数组不一定是矩阵\n",
    "# 矩阵两种方式：1. 二维数组 2. matrix对象\n",
    "# 矩阵运算必须满足：(m, n) * (n, k) = (m, k) m行n列只能和n行k列的矩阵进行运算\n",
    "ar1 = np.array([[1,2,3],[3,4,5]])  # (2, 3) ndarray\n",
    "ar2 = np.array([[1,2],[3,4],[5,6]])  # (3, 2) ndarray\n",
    "print(np.matmul(ar1, ar2)) # 二维数组 矩阵乘法\n",
    "print(np.dot(ar1,ar2)) # 与matmul相同\n",
    "print(ar1 @ ar2) # 等于上面两种矩阵乘法函数，两个ndarray数组不能用 * 实现矩阵乘法，矩阵乘法用@， * 是数组乘法必须满足广播协议\n",
    "# matrix 的矩阵对象\n",
    "m1 = np.matrix(ar1)\n",
    "m2 = np.matrix(ar2)\n",
    "print(m1 * m2) # 矩阵乘"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 120,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[ 0  2  4  6  8 10]\n",
      "[array([0, 2, 4]), array([ 6,  8, 10])]\n",
      "[array([0, 2]), array([4, 6]), array([ 8, 10])]\n"
     ]
    }
   ],
   "source": [
    "# 数组分割\n",
    "ar1 = np.arange(0, 11, 2) # 步长2，产生0-11间的数\n",
    "print(ar1)\n",
    "print(np.split(ar1, 2)) # 分割为两个数组每个数组3个数\n",
    "print(np.split(ar1, [2, 4])) # 以第2个索引，第4个索引分割数组"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 141,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[10.06331378 10.54979357  9.70703315]\n",
      " [ 9.45271094 10.53899793 10.40394143]\n",
      " [ 2.58604574  3.37164965  7.10689302]]\n",
      "[[10.06331378 10.54979357  9.70703315  2.58604574]\n",
      " [ 9.45271094 10.53899793 10.40394143  3.37164965]\n",
      " [ 2.58604574  3.37164965  7.10689302  7.10689302]]\n",
      "[[10.06331378 10.54979357  9.70703315  2.58604574]\n",
      " [ 9.45271094 10.53899793 10.40394143  3.37164965]\n",
      " [ 2.58604574  3.37164965  7.10689302  7.10689302]]\n"
     ]
    }
   ],
   "source": [
    "# 数组连接 横向 纵向, 连接的两个数组维度必须满足：横向则行必须相同，纵向则列必须相同\n",
    "ar1 = np.random.normal(loc=10, scale=1, size=(2,3))\n",
    "ar2 = np.random.uniform(0, 10, 3)\n",
    "ar3 = np.vstack((ar1, ar2))\n",
    "print(ar3) # 纵向2行3列 与 1行3列拼接\n",
    "ar4 = ar2.reshape(3, 1) # 转为3行1列\n",
    "ar5 = np.hstack((ar3, ar4)) # 3行3列 与 3行1列横向拼接\n",
    "print(ar5)\n",
    "ar6 = np.concatenate((ar3, ar4), axis=1) # 拼接，与hstack vstack相同只是需要指定按照x或y轴拼接\n",
    "print(ar6)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.1"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
